package com.ruoyi.energy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.domain.TcOut;
import com.ruoyi.energy.domain.vo.CarDayQualityVO;
import com.ruoyi.energy.domain.vo.TcOutWXVO;
import com.ruoyi.energy.service.ITcOutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 出场回调Controller
 *
 * @author cjie
 * @date 2021-11-30
 */
@Api(tags = " 出场回调")
@RestController
@RequestMapping("/tcOut")
public class TcOutController extends BaseController {
    @Autowired
    private ITcOutService tcOutService;

    /**
     * 查询出场回调列表
     */
    @ApiOperation(value="查询出场回调列表", notes="查询出场回调列表")
    @PreAuthorize(hasPermi = "energy:tcOut:list")
    @GetMapping("/list")
    public TableDataInfo list(TcOut tcOut) {
        startPage();
        List<TcOut> list = tcOutService.selectTcOutList(tcOut);
        return getDataTable(list);
    }

    /**
     * 微信查询出场回调列表
     */
    @ApiOperation(value="微信查询出场回调列表", notes="微信查询出场回调列表")
    @PreAuthorize(hasPermi = "energy:tcOut:list")
    @GetMapping("/wx/list")
    public TableDataInfo wxList(TcOutWXVO tcOut) {
        startPage();

        TcOut search = BeanUtil.toBean(tcOut,TcOut.class);
        List<TcOut> tcOuts = tcOutService.selectTcOutList(search);
        TableDataInfo tableData = getDataTable(tcOuts);

        List<TcOutWXVO> results = new ArrayList<>(tcOuts.size());
        for(TcOut tout:tcOuts){
            results.add(BeanUtil.toBean(tout,TcOutWXVO.class));
        }
        tableData.setRows(results);
        return tableData;
    }


    @ApiOperation(value="微信查询出场回调列表", notes="微信查询出场回调列表")
    @PreAuthorize(hasPermi = "energy:tcOut:list")
    @GetMapping("/wx/dayQualityData")
    public AjaxResult dayQualityData() {
        CarDayQualityVO result = tcOutService.selectDayQuality();
        return AjaxResult.success(result);
    }


    /**
     * 导出出场回调列表
     */
    @ApiOperation(value="导出出场回调列表", notes="导出出场回调列表")
    @PreAuthorize(hasPermi = "energy:tcOut:export")
    @Log(title = "出场回调", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TcOut tcOut) throws IOException {
        List<TcOut> list = tcOutService.selectTcOutList(tcOut);
        ExcelUtil<TcOut> util = new ExcelUtil<TcOut>(TcOut.class);
        util.exportExcel(response, list, "出场回调数据");
    }
    /**
     * 获取出场回调详细信息
     */
    @ApiOperation(value="获取出场回调详细信息", notes="获取出场回调详细信息")
    @PreAuthorize(hasPermi = "energy:tcOut:query")
    @GetMapping(value = "/{parkId}")
    public AjaxResult getInfo(@PathVariable("parkId") String parkId) {
        return AjaxResult.success(tcOutService.selectTcOutById(parkId));
    }

    /**
     * 新增出场回调
     */
    @ApiOperation(value="新增出场回调", notes="新增出场回调")
    @PreAuthorize(hasPermi = "energy:tcOut:add")
    @Log(title = "出场回调", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TcOut tcOut) {
        return toAjax(tcOutService.insertTcOut(tcOut));
    }

    /**
     * 修改出场回调
     */
    @ApiOperation(value="修改出场回调", notes="修改出场回调")
    @PreAuthorize(hasPermi = "energy:tcOut:edit")
    @Log(title = "出场回调", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TcOut tcOut) {
        return toAjax(tcOutService.updateTcOut(tcOut));
    }

    /**
     * 删除出场回调
     */
    @ApiOperation(value="删除出场回调", notes="删除出场回调")
    @ApiImplicitParam(name = "ids", value = "出场回调ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "energy:tcOut:remove")
    @Log(title = "出场回调", businessType = BusinessType.DELETE)
    @DeleteMapping("/{parkIds}")
    public AjaxResult remove(@PathVariable String[] parkIds) {
        return toAjax(tcOutService.deleteTcOutByIds(parkIds));
    }
}
